package com.xd.user.controller;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xd.common.model.R;
import com.xd.common.base.BaseController;
import com.xd.common.security.util.JwtParse;
import com.xd.common.security.util.SecurityUtils;
import com.xd.common.vo.Validity;
import com.xd.user.api.entity.Permission;
import com.xd.user.api.entity.RolePermission;
import com.xd.user.api.entity.User;
import com.xd.user.api.entity.UserRole;
import com.xd.user.api.feign.PermissionFeign;
import com.xd.user.service.PermissionService;
import com.xd.user.service.RolePermissionService;
import com.xd.user.service.RoleService;
import com.xd.user.service.UserRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 权限(Permission)表控制层
 *
 * @author hbx
 * @since 2019-12-30 17:17:21
 */
@Slf4j
@RestController
@RequestMapping("permission")
@Api
@Transactional(rollbackFor = Exception.class)
public class PermissionController extends BaseController<PermissionService, Permission, String> implements PermissionFeign {

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private RoleService roleService;

    @Resource
    private RolePermissionService rolePermissionService;


    @PostMapping("addPermission")
    public R addPermission(@RequestBody @Validated(Validity.Add.class) Permission permission){
        User user = SecurityUtils.getUser();
        if(this.service.save(permission)){
            List<UserRole> list = userRoleService.list(Wrappers.<UserRole>lambdaQuery().eq(UserRole::getUserId, user.getId()));
            List<RolePermission> collect = list.stream()
                    .map(userRole ->
                            new RolePermission().setPermissionId(permission.getId())
                                    .setRoleId(userRole.getRoleId())
                    )
                    .collect(Collectors.toList());
            rolePermissionService.saveBatch(collect);
        }else{
            return failed("添加失败");
        }
        return success("操作成功");
    }

    /**
     * @return com.baomidou.mybatisplus.extension.api.R<java.util.List < cn.hutool.core.lang.tree.Tree < java.lang.String>>>
     * @author huboxin
     * @description: TODO 获取菜单树
     * @date 10:22 2020/7/29
     **/
    @GetMapping("getTree")
    @ApiOperation(value = "获取菜单树", notes = "获取菜单树")
    public R<List<Tree<String>>> getTree() {
        List<Tree<String>> list = this.service.getTree();
        return success(list);
    }


    /**
     * @return java.util.List<java.lang.String>
     * @author huboxin
     * @description: TODO 获取被权限管理的所有路径
     * @date 10:32 2020/7/30
     **/
    @GetMapping("selectPermissionPaths")
    @ApiOperation(value = "获取被权限管理的所有路径", notes = "获取被权限管理的所有路径")
    @Override
    public List<String> selectPermissionPaths() {
        List<Object> objects = this.service.listObjs(Wrappers.<Permission>lambdaQuery().select(Permission::getPath));
        return objects.stream()
                .filter(ObjectUtil::isNotEmpty)
                .map(String::valueOf)
                .collect(Collectors.toList());
    }


    /**
     * @return R<List<Tree<String>>>
     * @author huboxin
     * @description: TODO 获取当前用户的权限树
     * @date 9:37 2020/8/19
     **/
    @ApiOperation(value = "获取当前用户的权限树", notes = "获取当前用户的权限树")
    @GetMapping("selectUserPermissionTree")
    public R<List<Tree<String>>> selectUserPermissionTree() {
        User user = SecurityUtils.getUser();
        List<Tree<String>> list = new ArrayList<>();
        if (!ObjectUtil.isEmpty(user)) {
            List<Permission> permissions = this.service.listByUserId(user.getId());
            list = this.service.getTree(permissions);
        }
        return success(list);
    }

}
